var express = require('express');
var router = express.Router();

var accountController = require('../controllers/AccountController.js');

/* GET users listing. */
router.get('/', function (req, res, next) {
    // res.send('respond with a resource');
    res.redirect('/'); // Go to web site root
});

router.post('/', function (req, res) {
    res.send('POST request to the homepage');
});

// POST /auth/login HTTP/1.1
router.post('/login', function (req, res) {
    
    console.log("req.body.input_email is    " + req.body.input_email);
    console.log("req.body.input_password is " + req.body.input_password);
    
    // Do validation 
    var idx;
    
    if (accountController.AuthenticateUser(req.body.input_email)) {
        res.redirect('/dashboard'); // Go to dasboard
    } else {
        res.redirect('/'); // Return to root page
    }
    
    
    
    
    // Debug: List req.params, req.body or req.query
    for (idx in req.params) {
        console.log("req.params[" + idx + "]: " + req.params[idx]);
    }
    for (idx in req.body) {
        console.log("req.body[" + idx + "]: " + req.body[idx]);
    }
    for (idx in req.query) {
        console.log("req.query[" + idx + "]: " + req.query[idx]);
    }
    
    //
    res.send('POST request to the auth-login');
});


router.get('/userlist', function (req, res) {
    // var db = req.db;
    // var collection = db.get('userlist');
    // collection.find({},{},function(e,docs){
    //     res.json(docs);
    // });
    var result = [];
    result.push({ 'username': 'user1' });
    result.push({ 'username': 'user2' });
    result.push({ 'username': 'user3' });
    result.push({ 'username': 'user4' });
    result.push({ 'username': 'user5' });

    res.json(result);
});

module.exports = router;
